﻿/*
@author liuzj
MeasureLength 测距
*/
define(["dojo/_base/declare"]
, function (declare) {
    var instance = declare(null, {
        
    });
    //订阅消息
    instance.action = function (map) {
	require(["esri/Color", "esri/graphic", "esri/geometry/Point", 
	         "esri/geometry/Polyline", "dojo/on",  "esri/toolbars/draw",
	         "esri/symbols/TextSymbol","esri/symbols/SimpleMarkerSymbol",
	         "esri/symbols/SimpleLineSymbol", "esri/symbols/PictureMarkerSymbol",
	         "esri/geometry/mathUtils"], 
	         function ( Color,Graphic, Point, Polyline, on,Draw,TextSymbol,MarkerSymbol,
	        		 SimpleLineSymbol,PictureMarkerSymbol,mathUtils) {
		console.log(map);
		var gOtherLayer=map.getLayer("other");
		
		var thispointsymble=new MarkerSymbol().setSize(9).setOutline(new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID, new Color([255, 0, 0]), 1),3).setColor(new Color([255, 255, 255, 1]));
		var thispolylinesymble= new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,new Color([251, 126, 67]),3);
			var suijishu=new Date().getTime();
			  function strLength(str) {
			        var realLength = 0, len = str.length, charCode = -1;
			        for (var i = 0; i < len; i++) {
			            charCode = str.charCodeAt(i);
			            if (charCode >= 0 && charCode <= 128) realLength += 1;
			            else realLength += 2;
			        }
			        return realLength;
			    }
	            function addResult(point, text) {
	                //添加起点标识
	                require(["esri/symbols/TextSymbol", "esri/symbols/PictureMarkerSymbol"], function (TextSymbol, PictureMarkerSymbol) {
	                    var len = strLength(text) * 5 + 20;
	                    var symbol = new TextSymbol(text).setColor(new Color([255, 0, 0]));
	                    symbol.setOffset(len / 2 + 20, -5);
	                    var graphic = new Graphic(point, symbol);
	                    graphic.bType = "MeasureLength"+suijishu;
	                    var pictureSymbol = new PictureMarkerSymbol({
		                        "url": "gistech/widgets/ManageTools/images/measure_bg.png",
		                        "height": 15,
		                        "width": len,
		                        "type": "esriPMS",
		                        "xoffset": len / 2 + 10,
		                        "yoffset": 0
								});
	                    var graphicB = new Graphic(point, pictureSymbol);
	                    graphicB.bType = "MeasureLength"+suijishu;
	                    gOtherLayer.add(graphicB);
	                    gOtherLayer.add(graphic);
	                });
	            }
	            var lastMapPoint = null;
	            var showPt = null;
	            var isfinish = 0;
	            var polyLine = new Polyline();
	          //测距完成响应事件
	            function drawfinish(length) {
	            	 var lengthtext = "";
	            	 if (length < 1000) {
	            		 lengthtext = "总长：" + (length).toFixed(1) + "米";
	                 }
	                 else {
	                	 lengthtext = "总长：" + (length/1000).toFixed(3) + "公里";
	                 }
	            	 if (isfinish == 1) {
	                     //增加一个关闭的事件
	                     var closeSymbol = new PictureMarkerSymbol({
	                         "url": "gistech/widgets/ManageTools/images/measure_close.png",
	                         "height": 12,
	                         "width": 12,
	                         "type": "esriPMS",
	                         "xoffset": 3,
	                         "yoffset": -10
	                     });
	                     var closeGraphic = new Graphic(showPt, closeSymbol);
	                     closeGraphic.bType = "MeasureLength"+suijishu;
	                     gOtherLayer.add(closeGraphic);
	                     var closeEvent=on(gOtherLayer, "click", function (evt) {
		                	 for( var i=gOtherLayer.graphics.length - 1;i>=0;i--){
		                    	 if (evt.graphic.bType ==gOtherLayer.graphics[i].bType) {
		                    		 gOtherLayer.remove(gOtherLayer.graphics[i]);
		                         }
		                    }
	                     });
	                 }
	                 addResult(showPt, lengthtext);
	                 showPt = null;
	            }
	            //区分单击和双击
	            var isdb = false;
	            var  mapDoubleClickEvent = on(map, "dbl-click", function (evt) {
	                var graphic = new Graphic(evt.mapPoint, thispointsymble);
	                graphic.bType = "MeasureLength"+suijishu;
	                gOtherLayer.add(graphic);
	                isdb = true;
	            });
	            var measureEvent = on(map, "click", function (evt) {
	                // remove listener after first event
	                isdb = false;
	                setTimeout(function () {
	                    if (isdb != false) {
	                        //移除双击事件
	                        if (mapDoubleClickEvent != null) {
	                            mapDoubleClickEvent.remove();
	                        }
	                        return;
	                    }
	                    var graphic = new Graphic(evt.mapPoint, thispointsymble);
	                    graphic.bType = "MeasureLength"+suijishu;
	                    gOtherLayer.add(graphic);
	                    if (lastMapPoint == null) {
	                        lastMapPoint = evt.mapPoint;
	                        addResult(lastMapPoint, "起点");
	                    }
	                    else {
	                        polyLine.spatialReference = map.spatialReference;
	                        polyLine.addPath([lastMapPoint, evt.mapPoint, evt.mapPoint]);
	                        //lengthParams.polylines = [polyLine];
	                        showPt = evt.mapPoint;
	                      //  EsriConfig.defaults.geometryService.lengths(lengthParams);
	                        lastMapPoint = evt.mapPoint;
	
	                    }
	                }, 500);
	                // do something else...
	            });
	            var drawTool =new Draw(map);
	            drawTool.activate(Draw["POLYLINE"]);
	            var drawEndEvent =on(drawTool,"draw-end", function (evt) {
	                isfinish = 1;
	                if (measureEvent != null) {
	                    measureEvent.remove();
	                }
	                if (mapDoubleClickEvent != null) {
	                	mapDoubleClickEvent.remove();
	                }
	                drawTool.deactivate();
	                map.setMapCursor("default");
	                var geometry = evt.geometry;
	                var length = geometry.paths[0].length;
	                showPt = new Point(geometry.paths[0][length - 1], map.spatialReference);
	                var graphic = new Graphic(geometry, thispolylinesymble);
	                graphic.bType = "MeasureLength"+suijishu;
	                gOtherLayer.add(graphic);
	                var alllength=0;
	                for(var i=1;i<length;i++)
	                	{
		                	var startpoint =new Point(geometry.paths[0][i-1], map.spatialReference);
		                	var endpoint =new Point(geometry.paths[0][i], map.spatialReference);
	                		alllength=alllength+mathUtils.getLength(startpoint,endpoint);
	                	}
	                drawfinish(alllength);
	                drawEndEvent.remove();
	                //绘制完成移除地图注册的事件
	            });
	        });
	    }
    return instance;
});